/**
 * 定义命名空间 
 */
Ext.ns("divo.admin.test")
Ext.ns("divo.admin.portal")
Ext.ns("divo.admin.user")
Ext.ns("divo.admin.group")
Ext.ns("divo.admin.menu")

Ext.ns("divo.xf.producer")
Ext.ns("divo.xf.projecter")
Ext.ns("divo.xf.checker")
Ext.ns("divo.xf.product")
Ext.ns("divo.xf.newproduct")
Ext.ns("divo.xf.queryproduct")
Ext.ns("divo.xf.checkreport")
Ext.ns("divo.xf.corpuser")
Ext.ns("divo.xf.mainter")
Ext.ns("divo.xf.advisor")
Ext.ns("divo.xf.auditproduct")
Ext.ns("divo.xf.querycheckreport")
//EOP
;/**
 * 新消防产品登记和提交核对
 */
divo.xf.newproduct.MainPanel = Ext.extend(divo.panel.PanelWithHtmlBtns, {
	initComponent : function() {
		if (!this.isProducerUser()) {
			alert("对不起，非生产厂家会员用户不能使用此功能。")
			divo.xf.newproduct.MainPanel.superclass.initComponent.call(this)
			return
		}
		this.portletDefinitions = [{
			id : 'info',
			text : '产品资料',
			xtype : "form.xf.ProductDisplayForm",
			config : {
			    menuItemId : this.menuItemId
			}
		},{
			id : 'actionlog',
			text : '操作日志',
			xtype : "panel.xf.ActionLogGridPanel",
			config : {
			    menuItemId : this.menuItemId,
			    actionCode : 5
			}
		}]
	
		this.productGridPanelId = this.myId("product")
		this.portalId = this.myId('portal')
		this.portalMgr = new Ext.ux.PortalManager(
			this.portalId,
			'xf-new-product-portal',
			this.portletDefinitions)
		
		Ext.apply(this, {
			layout : 'tdgi_border',
			defaults : {
				border : false
			},
			border : false,
			items : [{
				stateful : true,
				stateId : this.myId('product'),
				region : 'west',
				title : '新登记消防产品一览',
				collapsible : false,
				ctCls : "j-toolbar-dark",
				width : 600,
				collapsedTitle : true,
				split : true,
			    layout : 'fit',
                items : [this.getCenterContent()]
			}, {  
				region : 'center',
				layout : 'fit',
                tbar : this.portalMgr.getTbar(),
                autoScroll:true,
				ctCls : "j-toolbar-dark",
				items : [{
					xtype : 'statefulportal',
					id : this.portalId,
					items : [{
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}]
				}]
			}]	
		})

		this.subscribe("divo.rowSelect"+this.productGridPanelId,this.onProductChange,this)

		divo.xf.newproduct.MainPanel.superclass.initComponent.call(this)
	},
	onProductChange : function(subj, msg, data) {
		this.portalMgr.onRefresh(msg)
	},
	getCenterContent : function() {
		return new Ext.Panel({
			layout : 'border',
			defaults : {
				border : false
			},
			border : false,
			items : [{
				region : 'center',
				layout : 'fit',
				autoScroll : true,
				items : [{
                    id : this.productGridPanelId,
					xtype : "panel.xf.NewProductGridPanel",
					menuItemId : this.menuItemId
				}]
			}, this.getHtmlBtnsPanel({
					'new' : {
						image : 'new.png',
						fn :this.newProduct
					},
					'audit' : {
						image : 'allcheck.png',
						fn :this.auditProduct
					}
				})]
		})
	},
	newProduct : function() {
		Ext.getCmp(this.productGridPanelId).addItem()
	},
	auditProduct : function() {
		Ext.getCmp(this.productGridPanelId).auditItem()
	},
	initPortal : function() {
		this.portalMgr.init()

		Ext.ComponentMgr.get(this.portalId).on("drop", function() {
			this.portalMgr.saveState()
		},this)
	}
	
})

divo.xf.newproduct.Main = function() {
	/* ----------------------- private变量 ----------------------- */
	var mp

	/* ----------------------- private方法 ----------------------- */
	// 创建布局
	function createLayout(parentPanel,menuItemId,extraInfo) {
		mp = new divo.xf.newproduct.MainPanel({
			id : 'xf-newproduct',
			menuItemId : menuItemId
		})
		parentPanel.add(mp)
		parentPanel.doLayout()
	}

	/* ----------------------- public方法 ----------------------- */
	return {

		// 初始化
		init : function(parentPanel, menuItemId,extraInfo) {
			createLayout(parentPanel,menuItemId,extraInfo)
			mp.initPortal()
		}

	} // return

}()
// EOP

;/**
 * 定义命名空间 
 */
Ext.ns("divo.panel")

Ext.ns("form.test")
Ext.ns("panel.test")
Ext.ns("window.test")
Ext.ns("grid.test")
Ext.ns("tree.test")
Ext.ns("data.test")

Ext.ns("panel.admin")
Ext.ns("form.admin")
Ext.ns("window.admin")
Ext.ns("tree.admin")
Ext.ns("grid.admin")

Ext.ns("panel.xf")
Ext.ns("form.xf")
Ext.ns("window.xf")
Ext.ns("grid.xf")

//EOP
;/**
 * 消防产品网格面板
 */
panel.xf.NewProductGridPanel = Ext.extend(divo.panel.SmartGridDetailPanel, {
	initComponent : function() {
		this.corpInfo = this.getCorpInfo()
		this.action = new Ext.ux.grid.RowActions({
			header : '操作',
			autoWidth : false,
			width : 60,
			actions : [{
				iconCls : 'icon-audit',
				tooltip : '将消防产品提交核对',
				text : '提交核对',
				hideIndex : 'is_auditing'
			}, {
				iconCls : 'icon-update',
				text : '修改',
				tooltip : '修改消防产品',
				hideIndex : 'is_auditing'
			}, {
				iconCls : 'icon-delete',
				text : '删除',
				tooltip : '删除消防产品',
				hideIndex : 'is_auditing'
			}]
		})
		this.action.on('action', this.onAction, this)

		Ext.apply(this, {
			url : "/xf/products/new/producers/" + this.corpInfo.id,
			fieldDefinitions : this.getListDefinitions(),
			gridConfig : {
				gridIdOfView : 'xf-new-product-grid',
				filter : false,
				//toolbar : this.isLookup ? false : this.createToolbarBtn(),
				plugins : [this.action],
				autoExpandColumn : 'name'
			},
			autoScroll : true
		})

		this.subscribe("window.xf.ProductChanged", this.onProductChanged, this)
		this.on('beforedestroy', function() {
			this.unsubscribe()
		}, this)

		panel.xf.NewProductGridPanel.superclass.initComponent.apply(this,
				arguments)
	},
	onAction : function(grid, record, action, row, col) {
		if (action == 'icon-audit')
			this.simpleAuditItem()
		if (action == 'icon-update')
			this.editItem()
		if (action == 'icon-delete')
			this.deleteItem()
	},
	// 重写
	createGridMeta : function() {
		return this.getGridMeta(this.fieldDefinitions,this.isLookup?null:[this.action])
	},
	getListDefinitions : function() {
		return [{
			name : 'id'
		}, {
			name : 'name',
			header : '名称',
			searchable : true,
			type : 'string',
			id : 'name'
		}, {
			name : 'spec',
			header : '型号',
			type : 'string'
		}, {
			name : 'is_auditing',
			type : 'bool',
			convert : function(v, rec) {
				return rec.status == 1
			}
		}, {
			name : 'status_name',
			header : '产品状态',
			type : 'string'
		}]
	},
	addItem : function() {
		this.productWindowId = this.myId('win')

		var win = new window.xf.ProductAddOrEditWindow({
			id : this.productWindowId,
			closeAction : "destroy",
			cancelAction : "destroy",
			adding : true,
			productStatus : 0
		})
		win.show()
	},
	editItem : function() {
		if (!this.cId) {
			this.say("请先选择要修改的记录")
			return
		}
		this.productWindowId = this.myId('win')

		var win = new window.xf.ProductAddOrEditWindow({
			id : this.productWindowId,
			closeAction : "destroy",
			cancelAction : "destroy",
			adding : false,
			productId : this.cId
		})
		win.show()
	},
	onProductChanged : function(subj, msg, data) {
		if (msg.sender == this.productWindowId) {
			this.cIdFocus = msg.id
			this.loadList()
		}
	},
	onDeleteItem : function(id) {
		Ext.Ajax.request({
			scope : this,
			url : '/xf/products/' + id,
			method : 'DELETE',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				if (resp.success)
					this.deleteItemCallback()
				else
					this.say(resp.errors.reason)
			},
			failure : function(response, options) {
				this.alert(response.responseText)
			}
		})
	},
	auditItem : function() {
		var count = this.ds.getTotalCount()
		if (count==0) {
			this.say("请先添加新产品。")
			return
		}
		Ext.MessageBox.confirm("确认", "注意！提交将不可恢复！确定要提交吗？", 
								this.auditItemConfirm.createDelegate(this))
	},
	auditItemConfirm : function(btn) {
		if (btn != "yes") {
			return
		}
		Ext.Ajax.request({
			scope : this,
			method : 'PUT',
			url : this.url + '/status',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				if (resp.success)
					this.loadList()
				else
					this.say(resp.errors.reason)
			},
			failure : function(response, options) {
				this.alert(response.responseText) 
			}
		})
	},
	
	simpleAuditItem : function() {
		Ext.MessageBox.confirm("确认", "注意！提交将不可恢复！确定要提交吗？", 
								this.simpleAuditItemConfirm.createDelegate(this))
	},
	simpleAuditItemConfirm : function(btn) {
		if (btn != "yes") {
			return
		}
		Ext.Ajax.request({
			scope : this,
			method : 'PUT',
			url : "/xf/products/" + this.cId +'/status',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				if (resp.success)
					this.loadList()
				else
					this.say(resp.errors.reason)
			},
			failure : function(response, options) {
				this.alert(response.responseText) 
			}
		})
	}

})

Ext.reg('panel.xf.NewProductGridPanel', panel.xf.NewProductGridPanel);
// EOP

;/**
 * 操作日志显示网格面板
 */
panel.xf.ActionLogGridPanel = Ext.extend(divo.panel.SmartGridPanel, {
	actionCode : null,
	initComponent : function() {
		Ext.apply(this, {
			fieldDefinitions : this.getListDefinitions(),
			gridConfig : {
				gridIdOfView : 'xf-action-log-grid',
				displayLoadMask : false,
				footer : false,
				filter : false,
				allowGridState : false
			},
			autoScroll : true
		})
		
		panel.xf.ActionLogGridPanel.superclass.initComponent.apply(this,arguments)
	},
	getListDefinitions : function() {
		return [{
			name : 'id'
		},{
			header: "序号", 
			width: 40, 
			sortable : false,
			renderer: this.rowNumberRender.createDelegate(this)			
		},{
			name : 'descrip',
			header : '操作',
			type : 'string',
			sortable : false,
			width: 150, 
			renderer: this.wordWrapRender.createDelegate(this)
		},{
			name : 'user_name',
			header : '操作者',
			width: 100, 
			sortable : false,
			type : 'string'
		},{
			name : 'added_on',
			header : '操作时间',
			sortable : false,
			width: 150, 
			type : 'string'
		}]
	},
	//public
	showList : function(id) {
	   if (id)
	   	   this.objId = id
	   
	   if (!this.objId) return
	   
	   this.url = "/xf/actionlogs/"+this.actionCode+"/"+this.objId
	   this.setUrl(this.url)
  	   this.loadList()
	}
})

Ext.reg('panel.xf.ActionLogGridPanel', panel.xf.ActionLogGridPanel);
// EOP

;/**
 * 消防产品新建或修改表单
 */
form.xf.ProductAddOrEditForm = Ext.extend(divo.form.AddOrEditForm, {
	initFocusFldName : 'name',
	productId : null,
	productStatus : null, //0-新登记未提交审核 1-已提交审核等待审批 2-已经审核通过
	initComponent : function() {
		if (this.isProducerUser())
			this.corpInfo = this.getCorpInfo()
		var lovDisplay = [divo.t1 + '{name}' + divo.t2].join('');
		this.producerFld = {
			xtype : 'xlovfield',
			name : 'producer_id',
			fieldLabel : divo.required + '生产厂家',
			allowBlank : true,
			multiSelect : false,
			lovTitle : '请选择',
			lovHeight : 350,
			lovWidth : 550,
			minItem : 1,
			maxItem : 1,
			width : 300,
			valueField : 'id',
			displayField : lovDisplay,
			textarea : false,
			view : new panel.xf.ProducerGridPanel({
				isLookup : true
			})
		}
		var catReader = new Ext.data.JsonReader({
				totalProperty : 'totalCount',
				root : 'rows',
				id : 'id'
			}, [{
				name : 'tree_code'
			},{
				name : 'name'
			}])
		var cat1Store = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : '/xf/productcats/level1',
				method : 'GET'
			}),
			reader : catReader,
			autoLoad : true
		})
		if (!this.adding)
			cat1Store.on("load", function() {
				 this.afterCat1ComboLoad()
			}, this, true)
		var cat2Store = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				method : 'GET'
			}),
			reader : catReader,
			autoLoad : false
		})
		cat2Store.on("load", function() {
			 this.afterCat2ComboLoad()
		}, this, true)
		var list1 = [{
				xtype : 'panel',
				border : false,
				bodyStyle : 'background:none;padding-bottom:10px'
			}, {
				fieldLabel : "产品名称",
				name : "name",
				width : 300,
				listeners : {
					change : {
						scope : this,
						fn : function(o, value) {
							var py = Ext.getCmp(this.myId('py'))
							Ext.Ajax.request({
								scope : this,
								url : "/hz/pymemo",
								async : true,
								method : 'GET',
								params : {'name':o.getValue()}, 
								success : function(response, options) {
									var resp = Ext.decode(response.responseText)
									py.setValue(resp.hzpy)
								}
							})
						} //fn
					} //change
				} //listeners
			}, {
				fieldLabel : "产品型号",
				name : "spec",
				width : 300
			}, {
				fieldLabel : "拼音助记符",
				name : "py",
				id : this.myId('py'),
				width : 300
			}, {
				fieldLabel : "计量单位",
				name : "unit_name",
				width : 100
			}, new Ext.form.ComboBox({
				name : 'cat_code_1',
				fieldLabel : '一级分类',
				displayField : 'name',
				valueField : 'tree_code',
				hiddenName : 'cat_code_1',
				id : this.myId('cat1'),
				store : cat1Store,
				triggerAction : 'all',
				mode : 'local',
				editable : false,
				listeners : {
					select : {
						scope : this,
						fn : function(combo, value) {
							var cat2Combo = Ext.getCmp(this.myId('cat2'))
							cat2Combo.setValue('')
							cat2Combo.setDisabled(false)
							cat2Combo.store.proxy.conn.url = "/xf/productcats/childs/"+combo.getValue()
							cat2Combo.store.reload()
						}
					}
				}
			}), new Ext.form.ComboBox({
				name : 'cat_code_2',
				fieldLabel : '二级分类',
				displayField : 'name',
				valueField : 'tree_code',
				hiddenName : 'cat_code_2',
				id : this.myId('cat2'),
				disabled : this.adding,
				editable : false,
				store : cat2Store,
				triggerAction : 'all',
				mode : 'local',
				autoHeight : true
			})]
		var list2 = [{
				fieldLabel : "商标",
				name : 'brand',
				width : 300
			}, {
				xtype : "radiogroup",
				width : 240,
				fieldLabel : "认证类别",
				items: [
                    {boxLabel: '3C认证', name: 'auth_type',inputValue: '3C认证', checked: true},
                    {boxLabel: '型式认证', name: 'auth_type',inputValue: '型式认证'},
                    {boxLabel: '型式检验', name: 'auth_type',inputValue: '型式检验'}
                ]
			},{
				fieldLabel : "认证编号",
				name : 'auth_code',
				maxLength : 20,
				width : 180
			}, {
				xtype : 'datefield',
				fieldLabel : "认证有效期(截止日期)",
				name : 'end_date',
				format : 'Y.m.d',
				width : 100
			}, {
				xtype : 'datefield',
				fieldLabel : "报告签发日期",
				name : 'report_date',
				format : 'Y.m.d',
				width : 100
			}, {
				fieldLabel : "验证报告编号",
				name : 'report_code',
				maxLength : 20,
				width : 180
			}, {
				xtype : 'datefield',
				fieldLabel : "发换证日期",
				name : 'change_report_date',
				format : 'Y.m.d',
				width : 100
			}]
			
		var list3 = [this.producerFld]
		//登记新产品时不需要输入生产厂家
		var list = this.productStatus==0?list1.concat(list2):list1.concat(list3.concat(list2))
			
		Ext.apply(this, {
			labelWidth : 170,
			defaultType : "textfield",
			labelAlign : "right",
			autoScroll : true,
			bodyStyle : "padding:10px",
			items : list
		})
		form.xf.ProductAddOrEditForm.superclass.initComponent.call(this);
	},
	//重写
	loadItem : function() {
		Ext.Ajax.request({
			scope : this,
			url : "/xf/products/" + this.productId,
			method : 'GET',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText);
				this.getForm().setValues(resp.data)
				this.productStatus = resp.data.status

				var fld = this.getForm().findField("producer_id")
				if (fld)
					fld.setNameValue(resp.data.producer_name,resp.data.producer_id)
				
				this.cat_code1 = resp.data.cat_code_1
				this.cat_code2 = resp.data.cat_code_2
				
				var cat2Combo = Ext.getCmp(this.myId('cat2'))
				cat2Combo.store.proxy.conn.url = "/xf/productcats/childs/"+resp.data.cat_code_1
				cat2Combo.store.reload()
			},
			failure : function(response, options) {
				this.alert(response.responseText);
			}
		})
	},
	// public
	save : function(callbackOnSuccess) {
		var item = this.getForm().getObjectValues()
		item.status = this.productStatus
		if (this.productStatus==0)
			item.producer_id = this.corpInfo.id + ''
		
		var cat1Combo = Ext.getCmp(this.myId('cat1'))
		var cat2Combo = Ext.getCmp(this.myId('cat2'))
	    item.cat_name_1 = cat1Combo.el.dom.value
	    item.cat_name_2 = cat2Combo.el.dom.value

		Ext.Ajax.request({
			scope : this,
			url : "/xf/products" + (this.adding ? "" : "/" + this.productId),
			waitMsg : "正在提交...",
			method : (this.adding ? 'POST' : 'PUT'),
			jsonData : item,
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				if (!resp.success) {
					this.getForm().markInvalid(resp.errors)
				} else {
					callbackOnSuccess(resp.id)
				}
			},
			failure : function(response, options) {
				this.alert(response.responseText)
			}
		})
	},
	afterCat1ComboLoad : function() {
		if (!this.cat_code1) return
		
		var cat1Combo = Ext.getCmp(this.myId('cat1'))
		var i = cat1Combo.store.find("tree_code",this.cat_code1)
		var r = cat1Combo.store.getAt(i)
		cat1Combo.setValue(r.data.name)
	},		
	afterCat2ComboLoad : function() {
		var cat2Combo = Ext.getCmp(this.myId('cat2'))
		if (this.adding) {
            if (cat2Combo.store.getCount()==1) {
				var r = cat2Combo.store.getAt(0)
				cat2Combo.setValue(r.data.name)
			}
			return
		}
		var i = cat2Combo.store.find("tree_code",this.cat_code2)
		var r = cat2Combo.store.getAt(i)
		cat2Combo.setValue(r.data.name)
	}
})

Ext.reg("form.xf.ProductAddOrEditForm", form.xf.ProductAddOrEditForm)
// EOP

;/**
 * 消防产品完整信息显示用表单
 */
form.xf.ProductDisplayForm = Ext.extend(divo.form.DivoBasicForm, {
	initComponent : function() {
		Ext.apply(this, {
			formType : "readOnly",
			tableClass : "j-grid-table",
			readOnly : true,
			alternateRows : false,
			labelClass : "label",
			labelWidth : "30%",
			fieldDefinitions : this.getFormDefinitions()
		})
		form.xf.ProductDisplayForm.superclass.initComponent.call(this)
	},
	getFormDefinitions : function() {
		return [{
			field : "name",
			label : "产品名称",
			dataType : "string"
		}, {
			field : "producer_name",
			label : "产品生产单位",
			dataType : "string"
		}, {
			field : "spec",
			label : "产品型号",
			dataType : "string"
		}, {
			field : "unit_name",
			label : "计量单位",
			dataType : "string"
		}, {
			field : "cat_code_1",
			label : "产品分类",
			mapping : function(item) {
				return item.cat_name_1+"->"+item.cat_name_2
			},
			dataType : "string"
		}, {
			field : "brand",
			label : "商标",
			dataType : "string"
		}, {
			field : "auth_type",
			label : "认证类别",
			dataType : "string"
		}, {
			field : "auth_code",
			label : "认证编号",
			dataType : "string"
		}, {
			field : "end_date",
			label : "认证有效期",
			dataType : "string"
		}, {
			field : "report_date",
			label : "报告签发日期",
			dataType : "string"
		}, {
			field : "report_code",
			label : "验证报告编号",
			dataType : "string"
		}, {
			field : "change_report_date",
			label : "发换证报告日期",
			dataType : "string"
		}]
	},
	//public
	showList : function(id,msg) {
		if (!id) return
		Ext.Ajax.request({
			scope : this,
			url : "/xf/products/"+id,
			async : false,
			method : 'GET',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				this.setItem(resp.data)
			},
			failure : function(response, options) {
				this.alert(response.responseText)
			}
		})
	}
	
})

Ext.reg("form.xf.ProductDisplayForm", form.xf.ProductDisplayForm)
// EOP
;/**
 * 产品新建或修改窗口
 *
 * --------------------------------------------------------------
 * 消息：
 * 产品新增或修改了
 * 
 * 消息名：     			
 * window.xf.ProductChanged    
 * 
 * 消息内容：
 * {int} sender 本组件的id值
 * {int} id 新建或修改的记录id值
 * --------------------------------------------------------------
 */
window.xf.ProductAddOrEditWindow = Ext.extend(Ext.Window, {
	cancelAction : "hide",  //还可以取值：destroy
	closeAction : "hide",
	adding : false,
	productId : null,
	productStatus : null,
	initComponent : function() {
		var btns = [{
				text : "保存并继续",
				handler : this.onSave,
				scope : this
			}, {
				text : "保存并关闭",
				handler : this.onSaveAndClose,
				
				scope : this
			}, {
				text : "关闭",
				handler : this.onCancel,
				scope : this
			}]
		if (!this.adding)
			btns = [{
				text : "保存并关闭",
				handler : this.onSaveAndClose,
				scope : this
			}, {
				text : "关闭",
				handler : this.onCancel,
				scope : this
			}]
		Ext.apply(this,{
			title : (this.adding?'新建':'修改')+' 消防产品',
			width : 580,
			height : 520,
			modal : true,
			stateful : true,
			stateId : 'xf-product-win',
			closable : true,
			maximizable : false,
			minimizable : false,
			layout : "fit",
			buttons : btns,
			items : {
				xtype : "form.xf.ProductAddOrEditForm",
				adding : this.adding,
				productId : this.productId,
				productStatus : this.productStatus
			}
		})
		
		window.xf.ProductAddOrEditWindow.superclass.initComponent.call(this);
	},
	onSave : function() {
		this.items.itemAt(0).save(this.onSaveSucccess.createDelegate(this))
	},
	onSaveSucccess : function(id) {
		this.say(this.adding ? '新建成功, 请继续新建。':'修改已经保存')
		if (!this.adding)
		   this.close()
		else   
		   this.items.itemAt(0).clearForm()
		
		this.publish("window.xf.ProductChanged",{
			sender:this.id,
			id : id
		})
	},
	onSaveAndClose : function() {
		this.items.itemAt(0).save(this.onSaveAndCloseSucccess.createDelegate(this))
	},
	onSaveAndCloseSucccess : function(id) {
		this.say(this.adding ? '新建成功':'修改已经保存')
		this.close()
		
		this.publish("window.xf.ProductChanged",{
			sender:this.id,
			id : id
		})
	}
	
});
// EOP

;/**
 * 生产厂家网格面板
 */
panel.xf.ProducerGridPanel = Ext.extend(divo.panel.SmartGridDetailPanel, {
	defaultViewCode : null,
	initComponent : function() {
 		this.action = new Ext.ux.grid.RowActions({
            header:'操作',
            autoWidth:false, 
            width:60,
            actions:[{
                iconCls:'icon-update',
                tooltip:'修改生产厂家',
                hide : !this.canUpdate()
            },{
                iconCls:'icon-delete',
                tooltip:'删除生产厂家',
                hide : !this.canDelete()
            }]
        })
		this.action.on('action',this.onAction,this)
	
		Ext.apply(this, {
			url : "/xf/producers",
			fieldDefinitions : this.getListDefinitions(),
			gridConfig : {
				defaultViewCode : this.defaultViewCode,
				gridIdOfView : 'xf-producer-grid', 
				filter : true,
				toolbar : this.isLookup?false:this.createToolbarBtn(),
				plugins : [this.action],
				autoExpandColumn : 'name'
			},
			autoScroll : true
		})
		
		this.subscribe("window.xf.ProducerChanged", this.onProducerChanged, this)
		this.on('beforedestroy', function() {
			this.unsubscribe()
		}, this)
		
		panel.xf.ProducerGridPanel.superclass.initComponent.apply(this,arguments)
	},
	onAction : function(grid, record, action, row, col) {
		if (action=='icon-update')
		    this.editItem()
		if (action=='icon-delete')
		    this.deleteItem()
	},
	//重写
	createGridMeta : function() {
		return this.getGridMeta(this.fieldDefinitions,this.isLookup?null:[this.action])
	},
	getListDefinitions : function() {
		return [{
			name : 'id'
		},{
			name : 'name',
			header : '企业名称',
			searchable : true,
			type : 'string',
			id : 'name'
		},{
			name : 'py',
			header : '拼音助记符',
			searchable : true,
			type : 'string'
		},{
			name : 'area_name',
			header : '地区',
			searchable : true,
			type : 'string'
		}]
	},
	createToolbarBtn : function(){
		var tb = [];

		var addBtn = new Ext.Toolbar.Button( {
			text : '新建',
			tooltip : '新建生产厂家',
			icon : divo.iconAdd,
			cls : 'x-btn-text-icon',
			hidden : !this.canCreate(),
			handler : this.addItem.createDelegate(this)
		})
		tb.push(addBtn)
		return tb
	},
	addItem : function() {
		this.producerWindowId = this.myId('win')

		var win = new window.xf.ProducerAddOrEditWindow({
			id : this.producerWindowId,
			closeAction : "destroy",
			cancelAction : "destroy",
			adding : true
		})
		win.show()
	},
	editItem : function() {
		if (!this.cId) {
			this.say("请先选择要修改的记录")
			return
		}
		this.producerWindowId = this.myId('win')

		var win = new window.xf.ProducerAddOrEditWindow({
			id : this.producerWindowId,
			closeAction : "destroy",
			cancelAction : "destroy",
			adding : false,
			producerId : this.cId
		})
		win.show()
	},
	onProducerChanged : function(subj, msg, data) {
		if (msg.sender == this.producerWindowId) {
			this.cIdFocus = msg.id
			this.loadList()
		}
	},
	onLoadItem : function(id) {
		Ext.Ajax.request({
			scope : this,
			url : this.url+'/'+id,
			method : 'GET',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText);
				this.fillForm(resp.data)
			},
			failure : function(response, options) {
				this.alert(response.responseText);
			}
		})
	},
	onDeleteItem : function(id) {
		Ext.Ajax.request({
			scope : this,
			url : this.url+'/'+id,
			method : 'DELETE',
			success : function(response, options) {
				var resp = Ext.decode(response.responseText)
				if (resp.success)
					this.deleteItemCallback()
				else
				    this.sayError(resp.errors.reason) 
			},
			failure : function(response, options) {
				this.alert(response.responseText)
			}
		})
	}

})

Ext.reg('panel.xf.ProducerGridPanel', panel.xf.ProducerGridPanel);
// EOP

;/**
 * 消防产品查询
 */
divo.xf.queryproduct.MainPanel = Ext.extend(Ext.Panel, {
	initComponent : function() {
		this.portletDefinitions = [{
			id : 'info',
			text : '产品资料',
			xtype : "form.xf.ProductDisplayForm",
			config : {
			    menuItemId : this.menuItemId
			}
		},{
			id : 'actionlog',
			text : '操作日志',
			xtype : "panel.xf.ActionLogGridPanel",
			config : {
			    menuItemId : this.menuItemId,
			    actionCode : 5
			}
		}]
	
		this.productGridPanelId = this.myId("product")
		this.portalId = this.myId('portal')
		this.portalMgr = new Ext.ux.PortalManager(
			this.portalId,
			'xf-product-portal',
			this.portletDefinitions)
		
		Ext.apply(this, {
			layout : 'tdgi_border',
			defaults : {
				border : false
			},
			border : false,
			items : [{
				stateful : true,
				stateId : this.myId('product'),
				region : 'west',
				title : '消防产品查询结果一览',
				collapsible : false,
				ctCls : "j-toolbar-dark",
				width : 600,
				collapsedTitle : true,
				split : true,
				layout : 'fit',
				items : [{
					id : this.productGridPanelId,
					xtype : "panel.xf.QueryProductGridPanel",
					menuItemId : this.menuItemId
				}]
			}, {  
				region : 'center',
				layout : 'fit',
                tbar : this.portalMgr.getTbar(),
                autoScroll:true,
				ctCls : "j-toolbar-dark",
				items : [{
					xtype : 'statefulportal',
					id : this.portalId,
					items : [{
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}, {
						columnWidth : .25,
						style : 'padding:10px 0 10px 10px'
					}]
				}]
			}]	
		})

		this.subscribe("divo.rowSelect"+this.productGridPanelId,this.onProducerChange,this)

		divo.xf.queryproduct.MainPanel.superclass.initComponent.call(this)
	},
	onProducerChange : function(subj, msg, data) {
		this.portalMgr.onRefresh(msg)
	},
	initPortal : function() {
		this.portalMgr.init()

		Ext.ComponentMgr.get(this.portalId).on("drop", function() {
			this.portalMgr.saveState()
		},this)
	}
	
})

divo.xf.queryproduct.Main = function() {
	/* ----------------------- private变量 ----------------------- */
	var mp

	/* ----------------------- private方法 ----------------------- */
	// 创建布局
	function createLayout(parentPanel,menuItemId,extraInfo) {
		mp = new divo.xf.queryproduct.MainPanel({
			id : 'xf-queryproduct',
			menuItemId : menuItemId
		})
		parentPanel.add(mp)
		parentPanel.doLayout()
	}

	/* ----------------------- public方法 ----------------------- */
	return {

		// 初始化
		init : function(parentPanel, menuItemId,extraInfo) {
			createLayout(parentPanel,menuItemId,extraInfo)
			mp.initPortal()
		}

	} // return

}()
// EOP

;/**
 * 消防产品查询
 */
panel.xf.QueryProductGridPanel = Ext.extend(divo.panel.SmartGridPanel, {
	initComponent : function() {
		this.producerId = 0
		if (this.isProducerUser()) {
			this.producerId = this.getCorpInfo().id
		}
		
		Ext.apply(this, {
			url : "/xf/products/queryresult",
			fieldDefinitions : this.getListDefinitions(),
			gridConfig : {
				filter : true,
				gridIdOfView : 'xf-query-product-grid',
				displayLoadMask : false,
				footerSimple : true,
				autoExpandColumn : 'name'
			},
			autoScroll : true
		})

		panel.xf.QueryProductGridPanel.superclass.initComponent.apply(this,
				arguments)
	},
	getListDefinitions : function() {
		return [{
			name : 'id'
		}, {
			name : 'name',
			header : '名称',
			searchable : true,
			type : 'string',
			id : 'name'
		}, {
			name : 'spec',
			header : '型号',
			type : 'string'
		},{
			name : 'py',
			header : '拼音助记符',
			searchable : true,
			type : 'string'
		}]
	},
	//重写
	onBeforeLoad : function(store, options) {
		store.baseParams.producerId = this.producerId
	},
	//要求传baseParams时必须重写render方法
	render : function() {
		panel.xf.QueryProductGridPanel.superclass.render.apply(this, arguments)
		this.loadList()
	}
})

Ext.reg('panel.xf.QueryProductGridPanel', panel.xf.QueryProductGridPanel);
// EOP

;